package com.gaolang.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gaolang.dao.MedicineMapper;
import com.gaolang.dao.OrderFormMapper;
import com.gaolang.dao.ShopCarMapper;
import com.gaolang.dao.UserMapper;
import com.gaolang.pojo.Medicine;
import com.gaolang.pojo.OrderForm;
import com.gaolang.pojo.ShopCar;
import com.gaolang.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author DELLHL
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    UserMapper userMapper;

    @Autowired
    ShopCarMapper carMapper;

    @Autowired
    MedicineMapper medicineMapper;

    @Autowired
    OrderFormMapper orderFormMapper;

    @Override
    public boolean loginAuthentication(User user) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("username",user.getUsername());
        List<User> users = userMapper.selectByMap(map);
        for (User user1 : users) {
            if (user1.getPassword().equals(user.getPassword())){
                return true;
            }
        }
        return false;
    }

    @Override
    public int registerUser(User user) {

        return userMapper.insert(user);
    }

    @Override
    public int modifyUser(User user) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username",user.getUsername());
        User user2 = userMapper.selectOne(wrapper);
        user2.setBalance(user2.getBalance()-user.getBalance());
        return userMapper.updateById(user2);
    }

    @Override
    public List<Medicine> selectShopCar(Integer user_id) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("user_id",user_id);
        List<ShopCar> shopCars = carMapper.selectByMap(map);
        List<Integer> ids = new ArrayList<>();
        for (ShopCar shopCar : shopCars) {
            ids.add(shopCar.getMedId());
            System.out.println(shopCar.getMedId());
        }
        List<Medicine> medicines = medicineMapper.selectBatchIds(ids);

        for (int i = 0; i < medicines.size(); i++) {
            for (int j = 0; j < shopCars.size(); j++) {
                if (medicines.get(i).getId().equals(shopCars.get(j).getMedId())){
                    medicines.get(i).setAmount(shopCars.get(j).getQuantity());
                    break;
                }
            }

        }
        return medicines;
    }

    @Override
    public int selectIdByName(String username){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username",username);
        return userMapper.selectOne(wrapper).getId();
    }

    @Override
    public double orderMoney(Integer user_id) {
        double money = 0;
        List<Medicine> medicines = selectShopCar(user_id);
        for (int i = 0; i < medicines.size(); i++) {
            money += medicines.get(i).getPrice()*medicines.get(i).getAmount();
        }
        return money;
    }

    @Override
    public Medicine selectMedicine(String name) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("name",name);
        Medicine medicine = medicineMapper.selectOne(wrapper);
        return medicine;
    }

    @Override
    public List<Medicine> selectMedicine(String type,Integer num) {

        List<Medicine> medicines;
        if (!"".equals(type)){
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("type",type);
            medicines = medicineMapper.selectList(wrapper);
        }else {
            medicines = medicineMapper.selectList(null);
        }

        List<Medicine> medicineList = new ArrayList<>();
        for (int i = 0; i < num&&i < medicines.size(); i++) {
            medicineList.add(medicines.get(i));
        }
        return medicineList;
    }

    @Override
    public int addShopCar(ShopCar shopCar) {
        return carMapper.insert(shopCar);
    }

    @Override
    public int modifyShopNum(ShopCar shopCar) {
        QueryWrapper<ShopCar> wrapper = new QueryWrapper();
        wrapper
                .eq("med_id",shopCar.getMedId())
                .eq("user_id",shopCar.getUserId());
        ShopCar car = carMapper.selectOne(wrapper);
        car.setQuantity(shopCar.getQuantity());
        return carMapper.updateById(car);
    }

    @Override
    public int deleteShop(Integer user_id,Integer med_id) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("user_id",user_id);
        map.put("med_id",med_id);
        return carMapper.deleteByMap(map);
    }

    @Override
    public int addShop(ShopCar shopCar) {
        return carMapper.insert(shopCar);
    }

    @Override
    public User selectUserById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public int orderFormAdd(OrderForm orderForm) {
        return orderFormMapper.insert(orderForm);
    }

    @Override
    public List<Medicine> selectByName(String name) {
        QueryWrapper<Medicine> wrapper = new QueryWrapper<>();
        wrapper.like("name",name);
        List<Medicine> medicines = medicineMapper.selectList(wrapper);
        return medicines;
    }

    @Override
    public int modifyUser2(User user) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username",user.getUsername());
        User user2 = userMapper.selectOne(wrapper);
        user2.setBalance(user2.getBalance()+user.getBalance());
        return userMapper.updateById(user2);
    }

    @Override
    public List<OrderForm> order(Integer user_id){
        HashMap<String, Object> map = new HashMap<>();
        map.put("user_id",user_id);
        List<OrderForm> orderForms = orderFormMapper.selectByMap(map);
        return orderForms;
    }
}
